package client.comm.baoding.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.LayoutTxheaderBinding
import client.comm.baoding.ui.vm.TxViewModel
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.toast.toast
import com.bumptech.glide.RequestManager

class TxPagingHeaderAdapter(val manager: RequestManager, val context: Context) :
    LoadStateAdapter<PagingViewHoder>() {

    lateinit var userInfo: UserInfo

    lateinit var vm: TxViewModel

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): PagingViewHoder {
        return PagingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.layout_txheader,
                parent,
                false
            )
        ).apply {
            val itemBinding = this.binding as LayoutTxheaderBinding
            itemBinding.event = this@TxPagingHeaderAdapter

        }
    }

    override fun onBindViewHolder(holder: PagingViewHoder, loadState: LoadState) {
        val binding = holder.binding as LayoutTxheaderBinding
        binding.vm = vm
        binding.userInfo = userInfo
        binding.event = this

    }

    // true显示无论什么状态都显示
    override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
        return super.displayLoadStateAsItem(loadState) || (loadState.endOfPaginationReached)
    }

    var txCli: (() -> Unit)? = null
    var txzktCli: (() -> Unit)? = null

    fun tx() {
        if (vm.txNum.value.isNullOrEmpty()) {
            toast("请输入金额")
            return
        }
        txCli?.invoke()
    }

    fun txzkt() {
        if (vm.txNum.value.isNullOrEmpty()) {
            toast("请输入金额")
            return
        }
        txzktCli?.invoke()
    }
}